package com.changyi.device;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by xin on 2021/3/17
 * desc:
 */
public class TestXin {
    //写日志
    public static void writeLog(String err){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss:SSS");
        SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate)+":";
        String strContent=str+err+"\n";
        try {
            File dir = new File(Environment
                    .getExternalStorageDirectory().getPath()
                    + File.separator + "CYLog");
            if (!dir.exists())
                dir.mkdir();
            File file = new File(dir + File.separator + f.format(curDate) +"_"+"CY_log.txt" );
            if (!file.exists()) {
                file.createNewFile();
            }
            RandomAccessFile raf = new RandomAccessFile(file, "rw");
            raf.seek(file.length());
            raf.write(strContent.getBytes());
            raf.close();
        } catch (Exception e) {
            Log.e("TestFile", "Error on write File.");
        }
    }

}
